package cn.itcast.travel.dao.impl;

import cn.itcast.travel.dao.CommentDao;
import cn.itcast.travel.domain.Comment;
import cn.itcast.travel.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class CommentDaoImpl implements CommentDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 插入评论
     * @param comment
     */
    @Override
    public void addComment(Comment comment) {
        String sql = "insert into tab_comment value(null,?,?,?,?)";
        jdbcTemplate.update(sql,comment.getUid(),comment.getRid(),comment.getTime(),comment.getContent());
    }

    /**
     * 根据rid查询线路评论
     * @param rid
     * @return
     */
    @Override
    public List<Comment> getCommentByRid(int rid) {
        String sql = "select * from tab_comment where rid=?";
        List<Comment> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Comment>(Comment.class), rid);
        return query;
    }
}
